package com.controller.sys.user.login;

import com.common.ErrorCodeEnum;
import com.model.base.Result;
import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.StringUtils;
import com.core.springSecurity.model.Unlock;
import com.core.springSecurity.service.UnlockService;
import com.model.dto.sys.LoginController.VerifyImageCodeDTO;
import com.model.vo.sys.unlock.GetImageVerifyCodeVO;
import com.model.vo.sys.unlock.VerifyImageCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * @Author: zql
 * @Description:  验证码滑块生成服务
 * @Data: 16:41 2021/4/29
 */
@Api(tags = {"公共"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class UnlockController {

    @Resource
    UnlockService unlockService;

    /**
     * @Author: zql
     * @Description: 生成图形验证码
     * @Data: 15:47 2021/4/30
     */
    @ApiOperation(value = "生成图形验证码")
    @Log(title = "生成图形验证码", businessType = BusinessType.GRANT)
    @PostMapping("/getImageVerifyCode")
    public Result<GetImageVerifyCodeVO> getImageVerifyCode() {
        GetImageVerifyCodeVO getImageVerifyCodeVO =new GetImageVerifyCodeVO();
           Unlock unlock = unlockService.createImage();
            getImageVerifyCodeVO.setSmallImage(unlock.getSmallImage());
            getImageVerifyCodeVO.setBigImage(unlock.getBigImage());
            getImageVerifyCodeVO.setUnlockImageId(unlock.getUnlockImageId());
            getImageVerifyCodeVO.setYheight( unlock.getHeightRandom());
        return Result.suc(getImageVerifyCodeVO);
    }

    /**
     * @param verifyImageCodeDTO 移动距离
     * @return BaseRestResult 返回类型
     * @Description: 校验滑块拼图验证码
     */
    @ApiOperation(value = "校验滑块拼图验证码")
    @PostMapping("/verifyImageCode")
    public Result<VerifyImageCodeVO> verifyImageCode(@RequestBody @Valid VerifyImageCodeDTO verifyImageCodeDTO) {
        Double dMoveLength = Double.valueOf(verifyImageCodeDTO.getMoveLength());
        String unlockLogin =  unlockService.verifyImageCode(verifyImageCodeDTO.getUnlockImageId(),dMoveLength,"");
        if(StringUtils.isEmpty(unlockLogin)){
            return Result.fail(ErrorCodeEnum.UNLOCK_CODE_VERIFY_FAILED);
        }
        return Result.suc(VerifyImageCodeVO.builder().unlockLogin(unlockLogin).build());
    }
}
